]> git.saurik.com Git - apple/security.git/blob - OSX/Keychain Circle Notification/KNPersistentState.m
Security-57740.20.22.tar.gz
[apple/security.git] / OSX / Keychain Circle Notification / KNPersistentState.m
1 /*
2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 #import "KNPersistentState.h"
26
27 @implementation KNPersistentState
28
29 -(NSURL*)urlForStorage
30 {
31 return [NSURL URLWithString:@"Preferences/com.apple.security.KCN.plist" relativeToURL:[[NSFileManager defaultManager] URLForDirectory:NSLibraryDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil]];
32 }
33
34 +(instancetype)loadFromStorage
35 {
36 KNPersistentState *state = [KNPersistentState new];
37 if (!state) {
38 return state;
39 }
40
41 id plist = @{@"lastWritten": [NSDate distantPast]};
42
43 NSError *error = nil;
44 NSData *stateData = [NSData dataWithContentsOfURL:[state urlForStorage] options:0 error:&error];
45 if (!stateData) {
46 NSLog(@"Can't read state data (p=%@, err=%@)", [state urlForStorage], error);
47 } else {
48 NSPropertyListFormat format;
49 plist = [NSPropertyListSerialization propertyListWithData:stateData options: NSPropertyListMutableContainersAndLeaves format:&format error:&error];
50
51 if (plist == nil) {
52 NSLog(@"Can't deserialize %@, e=%@", stateData, error);
53 }
54 }
55
56 state.lastCircleStatus = plist[@"lastCircleStatus"] ? [plist[@"lastCircleStatus"] intValue] : kSOSCCCircleAbsent;
57 state.lastWritten = plist[@"lastWritten"];
58 state.pendingApplicationReminder = plist[@"pendingApplicationReminder"] ?: [NSDate distantFuture];
59 state.applicationDate = plist[@"applicationDate"] ?: [NSDate distantPast];
60 state.debugLeftReason = plist[@"debugLeftReason"];
61 state.pendingApplicationReminderInterval = plist[@"pendingApplicationReminderInterval"];
62 state.absentCircleWithNoReason = plist[@"absentCircleWithNoReason"] ? [plist[@"absentCircleWithNoReason"] intValue] : NO;
63
64 if (!state.pendingApplicationReminderInterval || [state.pendingApplicationReminderInterval doubleValue] <= 0) {
65 state.pendingApplicationReminderInterval = [NSNumber numberWithUnsignedInt: 24*60*60];
66 }
67
68 return state;
69 }
70
71 -(void)writeToStorage
72 {
73 NSMutableDictionary *plist = [@{@"lastCircleStatus" : [NSNumber numberWithInt:self.lastCircleStatus],
74 @"lastWritten" : [NSDate date],
75 @"applicationDate" : self.applicationDate,
76 @"pendingApplicationReminder" : self.pendingApplicationReminder,
77 @"pendingApplicationReminderInterval": self.pendingApplicationReminderInterval,
78 @"absentCircleWithNoReason" : [NSNumber numberWithBool:self.absentCircleWithNoReason],
79 } mutableCopy];
80 if (self.debugLeftReason)
81 plist[@"debugLeftReason"] = self.debugLeftReason;
82 NSLog(@"writeToStorage plist=%@", plist);
83
84 NSError *error = nil;
85 NSData *stateData = [NSPropertyListSerialization dataWithPropertyList:plist format:NSPropertyListXMLFormat_v1_0 options:kCFPropertyListImmutable error:&error];
86 if (!stateData) {
87 NSLog(@"Can't serialize %@: %@", plist, error);
88 return;
89 }
90 if (![stateData writeToURL:[self urlForStorage] options:NSDataWritingAtomic error:&error]) {
91 NSLog(@"Can't write to %@, error=%@", [self urlForStorage], error);
92 }
93 }
94
95
96 @end